import 'dart:convert';

import 'package:dio/dio.dart';
import 'package:super_study_language_tools/model/model_user_album.dart';
import 'package:super_study_language_tools/model/model_user_album_media.dart';
import 'package:super_study_language_tools/model/model_user_album_media_record.dart';

class ToolsNet {
  static Dio _dio = Dio();

  static String _token = "";

  // "Authorization:" "Bearer "+_token
  // Admin-Token=_token

  static setToken(String token) {
    _token = token;

    _dio.options.headers["Authorization"] = "Bearer " + _token;
    _dio.options.headers["Cookie"] = "Admin-Token=" + _token;
  }

  // JSON处理
  static dynamic parseJSON(String value) {
    String value2 = value.replaceAll("{", "{\"");
    value2 = value2.replaceAll(": ", "\":\"");
    value2 = value2.replaceAll(", ", "\",\"");
    value2 = value2.replaceAll("}", "\"}");
    print(value2);
    var obj = jsonDecode(value2);
    return obj;
  }

  // 1. 用户模块
  // 1.1. 获取验证码
  static Future<String> requestCode() async {
    Map<String, String> m = {};
    String url = "http://localhost:8080/captchaImage";
    Response response;
    response = await _dio.get(url);
    return response.data.toString().trim();
  }

  // 1.2. 用户登录
  static Future<String> requestLogin(Map<String, dynamic> m) async {
    String url = "http://localhost:8080/login";
    Response response;
    response = await _dio.post(url, data: m);
    return response.data.toString().trim();
  }

  // 1.3. 用户注册
  static Future<String> requestRegister(Map<String, dynamic> m) async {
    String url = "http://localhost:8080/register";
    Response response;
    response = await _dio.post(url, data: m);
    return response.data.toString().trim();
  }

  // 2 专辑模块
  // 2.1. 请求添加专辑
  static void requestAddAlbum(ModelUserAlbum? m, List<ModelUserAlbumMedia> ms) {}

  // 2.2 请求删除专辑
  static void requestAlbumRemove(int id) {}

  // 2.3 修改专辑信息
  static void requestAlbumUpdate(ModelUserAlbum? m) {}

  // 2.4 请求专辑列表
  static dynamic requestAlbumList() async {
    String url = "http://localhost:8080/user_useralbum/p001useralbum/list";
    Response response;
    response = await _dio.post(url);

    return response.data.toString().trim();
  }

  // 3. 专辑媒体模块
  // 3.1 请求修改专辑媒体信息
  static void requestAlbumMediaUpdate(ModelUserAlbumMedia m) {}
  // 3.2 请求请求专辑媒体列表
  static dynamic requestAlbumMediaList() {}

  // 4. 用户媒体学习记录模块
  // 4.1 请求添加
  static void requestAlbumMediaRecordAdd(ModelUserAlbumMediaRecord r) {}
  // 4.2 请求修改
  static void requestAlbumMediaRecordUpdate(ModelUserAlbumMediaRecord r) {}
  // 4.3 请求查询
  static dynamic requestAlbumMediaRecordList() {
    return null;
  }

  static dynamic requestAlbumMediaRecordById(int id) {
    return null;
  }

  // 5. 单词模块
  // 5.1 添加单词
  static void requestUserWordAdd() {}
  // 5.2 修改单词
  static void requestUserWordUpdate() {}
  // 5.3 删除单词
  static void requestUserWordRemove(int id) {}
  // 5.5 查询单词
  static dynamic requestUserWordList() {
    return null;
  }

  static dynamic requestUserWordById(int id) {
    return null;
  }

  // 6. 学习曲线模块
  // 6.1 查询学习曲线
  // 6.2 修改学习曲线
}
